/*
* File: gridp.java
* Author: Daniel Rogers
* Created on Jan 4, 2010
*
*/
import gri.tasks.Task;
import gri.tasks.TaskDef;
import gri.tasks.managers.TaskManager;
import gri.gridp.GridpFactory;
import java.util.Map;
import java.util.Iterator;
import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.PrintStream;
/**
* Command line program which allows one to run GRIDP programs.
* Module definitions are loaded from a folder and we can specify
* inputs in a parameter file or interactively (todo).
*/
public class gridp {
public static final int RUN_TASK = 1;
public static final int LIST_TASKS = 2;
public static final int DESCRIBE_TASK = 3;
// ------------------------------------------------------------ Properties
File moduleFolder;
String moduleName;
File paramFile;
int mode = RUN_TASK;
boolean windowsMode = true;
// ------------------------------------------------------------- Execution
public static void main(String [] args) {
gridp gridp = new gridp();
try {
boolean isValid = gridp.loadParams(args);
if (isValid)
gridp.execute();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void printHelp(PrintStream out) {
out.println();
out.println("GRIDP command line utility");
out.println();
out.println("Syntax:");
out.println();
out.println("gridp [parameters] [moduleName] [paramFile]");
out.println();
out.println(" moduleName Module ID to run/describe");
out.println(" paramFile File containing input parameters for module");
out.println();
out.println("Parameters:");
out.println(" --modules [folder]");
out.println(" Folder containing GRIDP XML modules to load");
out.println();
out.println("Run Mode Parameters:");
out.println(" --list List all modules");
out.println(" --describe Describe module");
out.println(" --run Run module (default)");
out.println(" --help Print help information");
out.println();
out.println("System Parameters:");
out.println(" --unix Run programs in Unix mode");
out.println(" --windows Run programs in Windows mode");
out.println();
}
public boolean loadParams(String [] args) {
//parse:
for (int i=0; i<args.length; i++) {
String arg = args[i];
if (arg.equals("--modules"))
moduleFolder = new File(args[++i]);
else if (arg.equals("--list"))
mode = LIST_TASKS;
else if (arg.equals("--describe"))
mode = DESCRIBE_TASK;
else if (arg.equals("--run"))
mode = RUN_TASK;
else if (arg.equals("--unix"))
windowsMode = false;
else if (arg.equals("--windows"))
windowsMode = true;
else if (arg.equals("--help")) {
printHelp(System.out);
return false;
}
//un-named params:
else if (moduleName == null)
moduleName = arg;
else if (paramFile == null)
paramFile = new File(arg);
else {
System.err.println("Unknown or unexpected parameter: " + arg);
printHelp(System.err);
return false;
}
}
//validate:
if (moduleFolder == null)
moduleFolder = new File(".");
if (!moduleFolder.exists()) {
System.err.println("Invalid module folder: " + moduleFolder.getPath() + ". Folder does not exist.");
return false;
}
//if (paramFile == null)
// throw new Exception("Missing argument: parameter file name.");
if (mode == RUN_TASK || mode == DESCRIBE_TASK) {
if (moduleName == null) {
System.err.println("Missing argument: module name.");
printHelp(System.err);
return false;
}
}
return true;
}
public void execute() throws Exception {
TaskManager taskManager = getTaskManager();
if (mode == LIST_TASKS) {
String [] taskIds = taskManager.getTaskIds();
System.out.println("Tasks:");
for (int i=0; i<taskIds.length; i++)
System.out.println(" " + taskIds[i]);
System.out.println();
}
else if (mode == DESCRIBE_TASK) {
TaskDef taskDef = taskManager.getTaskDefinition(moduleName);
System.out.println(taskDef);
}
else if (mode == RUN_TASK) {
Task task = taskManager.getTask(moduleName);
System.out.println("Loading parameters...");
Map inputs = parseInputs(task);
printParams(inputs, "Loaded:");
System.out.println("Executing...");
Map outputs = task.execute(inputs);
printParams(outputs, "Outputs:");
System.out.println("Job Complete!");
}
else
System.err.println("Unknown execution mode: " + mode);
}
protected TaskManager getTaskManager() {
int mode = windowsMode ?
GridpFactory.WINDOWS : GridpFactory.UNIX;
GridpFactory factory = new GridpFactory(moduleFolder, mode);
return factory.getTaskManager();
}
// ---------------------------------------------------- Parameters
public Map parseInputs(Task entry) throws Exception {
/*
Map params = new HashMap();
ParameterizedTaskDef taskDef = entry.getTaskDef();
if (taskDef.hasInputs()) {
//TODO:
//throw new RuntimeException("Not yet implemented");
}
return params;
*/
//Loads simple text properties
//TODO: load other properties using TaskDef
Properties properties = new Properties();
if (paramFile != null) {
FileInputStream in = new FileInputStream(paramFile);
properties.load(in);
in.close();
}
return properties;
}
public void printParams(Map map, String title) {
Iterator iEntries = map.entrySet().iterator();
Map.Entry entry;
System.out.println(title);
while (iEntries.hasNext()) {
entry = (Map.Entry)iEntries.next();
System.out.println(" " + entry.getKey() + " = " + entry.getValue());
}
System.out.println();
}
}